package com.zjsru.oneDay202204;

/**
 * @Author: likew
 * @Date: 2022/4/3
 *
 * 寻找比目标字母大的最小字母
 * 如果目标字母 target = 'z' 并且字符列表为 letters = ['a', 'b']，则答案返回 'a'
 *
 * 输入: letters = ["c", "f", "j"]，target = "a"
 * 输出: "c"
 *
 * 输入: letters = ["c","f","j"], target = "d"
 * 输出: "f"
 */
public class nextGreatestLetter {
    public char nextGreatestLetter(char[] letters, char target) {
        int left = 0 ,right = letters.length -1;
        while(right > left){
            int mid = (right - left)/2 + left;
            if (target < letters[mid]){
                right = mid;
            }else {
                left = mid +1;
            }
        }
        return letters[right] > target ? letters[right] : letters[0];
    }
    
    public static void main(String[] args) {
        nextGreatestLetter nextGreatestLetter =new nextGreatestLetter();
        char[] letters = new char[]{'c', 'f', 'j'};
        char target = 'c';
        System.out.println(nextGreatestLetter.nextGreatestLetter(letters, target));
    }
}
